package com.ghh.listener;

import com.alibaba.fastjson.JSON;
import com.ghh.Repostitory.GoodsRepostitory;
import com.ghh.domain.Goods;
import com.ghh.mapper.GoodsMapper;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

@Component
public class RabbimtMQListener {

    @Autowired
    private GoodsRepostitory goodsRepostitory;

    @Autowired
    private GoodsMapper goodsMapper;

    @RabbitListener(queues = "sync_data")
    public void ListenerQueue(Message message) {

        //1.查询所有数据，mysql
        List<Goods> goodsList = goodsMapper.findAll();

        for (Goods goods : goodsList) {
            //2.2 设置spec规格信息 Map的数据   specStr:{}
            String specStr = goods.getSpecStr();
            //将json格式字符串转为Map集合
            Map map = JSON.parseObject(specStr, Map.class);
            //设置spec map
            goods.setSpec(map);
        }
        //2.批量插入
        goodsRepostitory.saveAll(goodsList);

    }
}
